/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vista;

import DAO.CopiaArchivo;
import controller.*;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.jvnet.substance.SubstanceLookAndFeel;

/**
 *
 * @author ArandiLopez
 */
public class VistaProyecto extends javax.swing.JFrame {

    //Atributos nuevos
    private File raiz, separado;
    private ArrayList<String> lista;
    private ControllerDAO controlDao;
    private InternalSortController controlInterno;
    private ExternalSortController controlExterno;
    private final CopiaArchivo copiador;

    /**
     *
     * @return
     */
    public File getRaiz() {
        return this.raiz;
    }

    /**
     *
     * @param raiz
     */
    public void setRaiz(File raiz) {
        this.raiz = raiz;
    }

    /**
     *
     * @return
     */
    public File getSeparado() {
        return separado;
    }

    /**
     *
     * @param separado
     */
    public void setSeparado(File separado) {
        this.separado = separado;
    }

    /**
     * Creates new form VistaProyecto
     */
    public VistaProyecto() {


        initComponents();
        nombreDocLb.setText("         ");
        cargadoLb.setText("No se ha cargado ningun archivo ");
        controlDao = new ControllerDAO();
        controlInterno = new InternalSortController();
        controlExterno = new ExternalSortController();
        copiador = new CopiaArchivo();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jPopupMenu2 = new javax.swing.JPopupMenu();
        TabPane = new javax.swing.JTabbedPane();
        internoPanel = new javax.swing.JPanel();
        ordenarInternoBtn = new javax.swing.JButton();
        shellPanel = new javax.swing.JPanel();
        ordenarShellBtn = new javax.swing.JButton();
        externoPanel = new javax.swing.JPanel();
        ordenarExternoBtn = new javax.swing.JButton();
        cargarArchivoBtn = new javax.swing.JButton();
        nombreDocLb = new javax.swing.JLabel();
        cargadoLb = new javax.swing.JLabel();
        aboutBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Algoritmos de Ordenacion");
        setResizable(false);

        TabPane.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
        TabPane.setName("JTabbedPane"); // NOI18N

        ordenarInternoBtn.setFont(new java.awt.Font("Cantarell", 0, 24)); // NOI18N
        ordenarInternoBtn.setText("Ordenar");
        ordenarInternoBtn.setToolTipText("Presione para ordenar.");
        ordenarInternoBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ordenarInternoBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout internoPanelLayout = new javax.swing.GroupLayout(internoPanel);
        internoPanel.setLayout(internoPanelLayout);
        internoPanelLayout.setHorizontalGroup(
            internoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(internoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ordenarInternoBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(45, Short.MAX_VALUE))
        );
        internoPanelLayout.setVerticalGroup(
            internoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(internoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ordenarInternoBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        TabPane.addTab("Interno", internoPanel);

        ordenarShellBtn.setFont(new java.awt.Font("Cantarell", 0, 24)); // NOI18N
        ordenarShellBtn.setText("Ordenar");
        ordenarShellBtn.setToolTipText("Presione para ordenar");
        ordenarShellBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ordenarShellBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout shellPanelLayout = new javax.swing.GroupLayout(shellPanel);
        shellPanel.setLayout(shellPanelLayout);
        shellPanelLayout.setHorizontalGroup(
            shellPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(shellPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ordenarShellBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(45, Short.MAX_VALUE))
        );
        shellPanelLayout.setVerticalGroup(
            shellPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(shellPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ordenarShellBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        TabPane.addTab("Shellsort", shellPanel);

        externoPanel.setBorder(null);

        ordenarExternoBtn.setFont(new java.awt.Font("Cantarell", 0, 24)); // NOI18N
        ordenarExternoBtn.setText("Ordenar");
        ordenarExternoBtn.setToolTipText("Presione para ordenar");
        ordenarExternoBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ordenarExternoBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout externoPanelLayout = new javax.swing.GroupLayout(externoPanel);
        externoPanel.setLayout(externoPanelLayout);
        externoPanelLayout.setHorizontalGroup(
            externoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(externoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ordenarExternoBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(45, Short.MAX_VALUE))
        );
        externoPanelLayout.setVerticalGroup(
            externoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(externoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ordenarExternoBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );

        TabPane.addTab("Externo", externoPanel);

        cargarArchivoBtn.setText("Cargar Archivo");
        cargarArchivoBtn.setToolTipText("Presione para cargar un archivo de texto que desee ordenar");
        cargarArchivoBtn.setName(""); // NOI18N
        cargarArchivoBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cargarArchivoBtnActionPerformed(evt);
            }
        });

        cargadoLb.setText("Archivo Cargado:");

        aboutBtn.setText("About");
        aboutBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(TabPane, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(aboutBtn))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cargadoLb)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(nombreDocLb, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cargarArchivoBtn))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(cargarArchivoBtn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cargadoLb, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(nombreDocLb, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TabPane, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(aboutBtn)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cargarArchivoBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cargarArchivoBtnActionPerformed
        // TODO add your handling code here:
        ControlFilter controlFilter = new ControlFilter("Archivo de texto .txt", "txt");
        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setMultiSelectionEnabled(false);
        chooser.setCurrentDirectory(new File("./"));
        chooser.setFileFilter(controlFilter.getFilter());
        chooser.setAcceptAllFileFilterUsed(false);
        chooser.showOpenDialog(this);
        try {
            setRaiz(chooser.getSelectedFile());
            cargadoLb.setText("Archivo Cargado: ");
            nombreDocLb.setText(getRaiz().getName());

            this.lista = controlDao.readFile(getRaiz());
            controlInterno.setArregloList(lista);
            this.separado = controlDao.writeFile(lista, "separado");
            copiador.createCopyFile(separado, "directa.txt");
            copiador.createCopyFile(separado, "natural.txt");
            copiador.createCopyFile(separado, "equi.txt");

            JOptionPane.showMessageDialog(this, "¡Archivo cargado y separado con exito!");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Archivo no cargado");
        }

    }//GEN-LAST:event_cargarArchivoBtnActionPerformed

    private void ordenarInternoBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ordenarInternoBtnActionPerformed
        // TODO add your handling code here:
        Graficador graficador;
        graficador = new Graficador("Ordenacion Interna", "Algoritmos de Ordenacion Interna", "Milisegundos");
        graficador.setData(controlInterno.getMilisInsercion(), "Milisegundos", "Insercion");
        graficador.setData(controlInterno.getMilisBurbujaMenor(), "Milisegundos", "Burbuja Menor");
        graficador.setData(controlInterno.getMilisBurbujaMayor(), "Milisegundos", "Burbuja Mayor");
        graficador.setData(controlInterno.getMilisMergeSort(), "Milisegundos", "Mergesort");
        graficador.setData(controlInterno.getMilisQuickSort(), "Milisegundos", "Quicksort");
        graficador.drawGraphics();


    }//GEN-LAST:event_ordenarInternoBtnActionPerformed

    private void ordenarShellBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ordenarShellBtnActionPerformed
        // TODO add your handling code here:
        Graficador graficador;
        graficador = new Graficador("Ordenacion por Shellsort", "Algoritmos de Shellsort", "Milisegundos");
        graficador.setData(controlInterno.getMilisShellIncrementoNormal(), "Milisegundos", "Incremento Normal");
        graficador.setData(controlInterno.getMilisShellSerieFrank(), "Milisegundos", "Serie de Frank");
        graficador.setData(controlInterno.getMilisShellSerieHibberd(), "Milisegundos", "Serie de Hibberd");
        graficador.setData(controlInterno.getMilisShellSeriePapernov(), "Milisegundos", "Serie de Papernov");
        graficador.drawGraphics();
    }//GEN-LAST:event_ordenarShellBtnActionPerformed

    private void ordenarExternoBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ordenarExternoBtnActionPerformed
        // TODO add your handling code here:
        Graficador graficador = new Graficador("Ordenacion Externa", "Algortimos de Ordenacion Externa", "Milisegundos");
        graficador.setData(controlExterno.getMilisMezclaDirecta(), "Milisegundos", "Mezcla Directa");
        graficador.setData(controlExterno.getMilisMezclaNatural(), "Milisegundos", "Mezcla Natural");
        graficador.setData(controlExterno.getMilisMezclaEquilibrada(), "Milisegundos", "Mezcla Equilibrada");

        graficador.drawGraphics();
    }//GEN-LAST:event_ordenarExternoBtnActionPerformed

    private void aboutBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutBtnActionPerformed
        // TODO add your handling code here:
        Licencia licencia = new Licencia(this, false);
        licencia.setVisible(true);
    }//GEN-LAST:event_aboutBtnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {


        SubstanceLookAndFeel.setSkin("org.jvnet.substance.skin.RavenSkin");
        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new VistaProyecto().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane TabPane;
    private javax.swing.JButton aboutBtn;
    private javax.swing.JLabel cargadoLb;
    private javax.swing.JButton cargarArchivoBtn;
    private javax.swing.JPanel externoPanel;
    private javax.swing.JPanel internoPanel;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JPopupMenu jPopupMenu2;
    private javax.swing.JLabel nombreDocLb;
    private javax.swing.JButton ordenarExternoBtn;
    private javax.swing.JButton ordenarInternoBtn;
    private javax.swing.JButton ordenarShellBtn;
    private javax.swing.JPanel shellPanel;
    // End of variables declaration//GEN-END:variables
}
